package cn.iocoder.yudao.framework.common.enums.pay;

import cn.hutool.core.util.ArrayUtil;
import cn.iocoder.yudao.framework.common.core.ArrayValuable;
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

/**
 * 支付订单的状态枚举
 *
 * @author 芋道源码
 */
@Getter
@AllArgsConstructor
public enum PayOrderStatusEnum implements ArrayValuable<Integer> {

    WAITING(0, "未支付"),
    PART_PAY(10, "已付定金"),
    UNCLEAR(20, "已结算未结清"),
    CLEAR(30, "已结清"),
    REFUND(40, "已退款"),
    CLOSED(50, "支付关闭"), // 注意：全部退款后，还是 REFUND 状态
    ;

    private final Integer status;
    private final String name;

    public static final Integer[] ARRAYS = Arrays.stream(values()).map(PayOrderStatusEnum::getStatus).toArray(Integer[]::new);


    public static boolean isClear(Integer status) {
        return Objects.equals(status, CLEAR.getStatus());
    }
    public static boolean isSettle(Integer status) {
        return Objects.equals(status, CLEAR.getStatus())
                || Objects.equals(status, UNCLEAR.getStatus());
    }

    public static boolean isOrderSettled(Integer status) {
        return isOrderSuccess(status) || Objects.equals(status, UNCLEAR.getStatus());
    }

    @Override
    public Integer[] array() {
        return ARRAYS;
    }
    public static PayOrderStatusEnum valueOf(Integer type) {
        if (type == null) {
            return null;
        }
        return ArrayUtil.firstMatch(item -> item.getStatus().equals(type), values());
    }

    /**
     * 判断是否等待支付
     *
     * @param status 状态
     * @return 是否等待支付
     */
    public static boolean isWaiting(Integer status) {
        return Objects.equals(status, WAITING.getStatus());
    }

    /**
     * 判断是否支付成功
     *
     * @param status 状态
     * @return 是否支付成功
     */
    public static boolean isOrderSuccess(Integer status) {
        return Objects.equals(status, CLEAR.getStatus()) ||
                Objects.equals(status, REFUND.getStatus()) ||
                Objects.equals(status, CLOSED.getStatus());
    }

    /**
     * 判断是否支付成功或者已退款
     *
     * @param status 状态
     * @return 是否支付成功或者已退款
     */
    public static boolean isSuccessOrRefund(Integer status) {
        return ObjectUtils.equalsAny(status,
                CLEAR.getStatus(), REFUND.getStatus());
    }

    /**
     * 判断是否支付关闭
     *
     * @param status 状态
     * @return 是否支付关闭
     */
    public static boolean isClosed(Integer status) {
        return Objects.equals(status, CLOSED.getStatus());
    }

}
